package com.jumi.microservice.controller.logistics.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.utils.DateUtil;
import com.jumi.microservice.dto.ExpressResponse;
import com.jumi.microservice.dto.logistics.Logistics;
import com.jumi.microservice.dto.logistics.LogisticsSearchRequest;
import com.jumi.microservice.dto.logistics.LogisticsSearchResponse;
import com.jumi.microservice.entity.LogisticsGoods;
import com.jumi.microservice.entity.UserOrder;
import com.jumi.microservice.enumerate.LogisticsStatusEnum;
import com.jumi.microservice.mapper.LogisticsGoodsMapper;
import com.jumi.microservice.mapper.UserOrderMapper;
import com.jumi.microservice.rocketmq.SendMessageHandler;
import com.jumi.microservice.service.IOrderService;
import com.jumi.microservice.service.logistics.express.ExpressTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author by mazhao
 * @Classname LogisticsController
 * @Description TODO
 * @Date 2020/8/21 15:28
 */
@RestController
@RequestMapping("api/logistics")
@Api(tags = {"C端-快递查询接口"})
public class ApiLogisticsController extends BaseController {

    @Resource
    ExpressTool expressTool;
    @DubboReference
    IOrderService iOrderService;
    @Resource
    LogisticsGoodsMapper logisticsGoodsMapper;
    @Resource
    UserOrderMapper userOrderMapper;

    @ApiOperation("快递查询")
    @PostMapping("/expressSearch")
    public ResponseResult<List<LogisticsSearchResponse>> searchExpress(LogisticsSearchRequest request) throws Exception {
        List<LogisticsSearchResponse> result = new ArrayList<>();
        QueryWrapper<LogisticsGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("real_logistics_no,express_code").eq("order_no", request.getOrderNo())
                .groupBy("real_logistics_no").eq("del_flag", 0)
                .ge("logistics_status", LogisticsStatusEnum.PART_SEND.getCode());
        List<LogisticsGoods> logisticsGoodsList = logisticsGoodsMapper.selectList(queryWrapper);
        QueryWrapper<UserOrder> userOrderQueryWrapper = new QueryWrapper<>();
        userOrderQueryWrapper.eq("order_no",request.getOrderNo()).last("limit 1");
        UserOrder userOrder = userOrderMapper.selectOne(userOrderQueryWrapper);
        String payTime = DateUtil.format(userOrder.getPayTime(),"yyyy-MM-dd HH:mm:ss");
        if (logisticsGoodsList.isEmpty()) {
            LogisticsSearchResponse logisticsSearchResponse = new LogisticsSearchResponse();
            Logistics logistics = new Logistics();
            logistics.setDesc("商品正在出库中");
            logistics.setDate("");
            logisticsSearchResponse.getList().add(logistics);
            logistics = new Logistics();
            logistics.setDesc("订单已提交");
            logistics.setDate(payTime);
            logisticsSearchResponse.getList().add(logistics);
            logisticsSearchResponse.setLogisticsName("快递公司");
            result.add(logisticsSearchResponse);
            return success(result);
        }
        for (LogisticsGoods logisticsGoods : logisticsGoodsList) {
            request.setLogisticsNo(logisticsGoods.getRealLogisticsNo());
            request.setExpressCode(logisticsGoods.getExpressCode());
            LogisticsSearchResponse logisticsSearchResponse = expressTool.apiSearch(request,payTime);
            result.add(logisticsSearchResponse);
        }
        return success(result);
    }

    @ApiOperation("快递单号自动识别")
    @PostMapping("/autoIdentify")
    public ResponseResult<ExpressResponse> autoIdentify(LogisticsSearchRequest request){
        return success(iOrderService.autoIdentify(request.getLogisticsNo()));
    }
}
